package cn.jasonone.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.jasonone.bean.SysResource;
import cn.jasonone.bean.SysResourceNode;
import cn.jasonone.bean.SysResourceType;
import cn.jasonone.service.SysResourceService;
import cn.jasonone.service.SysResourceTypeService;

@RestController
@RequestMapping("/resource")
public class SysResourceController {
	@Resource
	private SysResourceService sysResourceService;
	@Resource
	private SysResourceTypeService resourceTypeService;

	@GetMapping
	public ModelAndView home() {
		ModelAndView modelAndView = new ModelAndView("sys/resource/index");
		List<SysResourceType> types = resourceTypeService.findAll(null);
		modelAndView.addObject("types", types);
		return modelAndView;
	}

	@GetMapping({ "/save", "/save/{id}" })
	public ModelAndView save(@PathVariable(name = "id", required = false) Integer id, Integer pid) {
		ModelAndView modelAndView = new ModelAndView("sys/resource/save");
		if (id != null) {
			modelAndView.addObject("resource", sysResourceService.findById(id));
		} else if (pid != null) {
			SysResource resource = new SysResource();
			resource.setPid(pid);
			SysResource parent = sysResourceService.findById(pid);
			resource.setParent(parent);
			modelAndView.addObject("resource", resource);
		}
		List<SysResourceType> types = resourceTypeService.findAll(null);
		modelAndView.addObject("types", types);
		return modelAndView;
	}

	@PostMapping("/save")
	public Object save(SysResource resource) {
		Map<String, Object> result = new HashMap<>();
		result.put("success", sysResourceService.save(resource));
		return result;
	}

	@GetMapping(path = "/find", params = "tree")
	public Object findAll(SysResource resource,Integer roleId) {
		Map<String,Object> rs=new HashMap<>();
		List<SysResource> list = sysResourceService.findAll(resource);
		List<Integer> selectResourceIds=new ArrayList<>();
		if(roleId != null) {
			List<SysResource> roleRes = sysResourceService.findByRoleId(roleId);
			for (SysResource sysResource : roleRes) {
				selectResourceIds.add(sysResource.getId());
			}
		}
		rs.put("list", list);
		rs.put("checkeds", selectResourceIds);
		return rs;
	}

	@GetMapping("/find")
	public Object findAll(SysResource resource, String pname, @RequestParam(defaultValue = "1") int page,
			@RequestParam(defaultValue = "10") int limit) {
		if (pname != null && !pname.isEmpty()) {
			// 根据父资源名称查询父资源对象
			SysResource sysResource = new SysResource();
			sysResource.setName(pname);
			List<SysResource> list = sysResourceService.findAll(sysResource);
			if (!list.isEmpty()) {
				sysResource = list.get(0);// 得到父资源对象
				if (resource == null) {
					resource = new SysResource();
				}
				// 将父资源对象的ID,赋值给带查询参数中的PID
				resource.setPid(sysResource.getId());
			}
		}
		PageHelper.startPage(page, limit);
		List<SysResource> list = sysResourceService.findAll(resource);
		PageInfo<SysResource> pageInfo = new PageInfo<>(list);
		Map<String, Object> result = new HashMap<>();
		result.put("code", 0);
		result.put("msg", "查询成功");
		result.put("count", pageInfo.getTotal());
		result.put("data", pageInfo.getList());
		return result;
	}

	@GetMapping("/del")
	public Object delete(SysResource resource) {
		Map<String, Object> result = new HashMap<>();
		result.put("success", true);
		int id = resource.getId();
		resource.setId(null);
		resource.setPid(id);
		List<SysResource> list = sysResourceService.findAll(resource);

		if (list.isEmpty()) {
			if (!this.sysResourceService.deleteByIds(id)) {
				result.put("success", false);
				result.put("msg", "删除失败或数据不存在");
			}
		} else {
			result.put("success", false);
			result.put("msg", "当前删除的资源拥有子资源,无法删除");
		}
		return result;
	}

}
